/*
 * @Autor: 张小鹏
 * @Date: 2020-04-17 15:41:30
 * @LastEditors: 张小鹏
 * @LastEditTime: 2020-04-26 16:03:40
 * @description: 
 */
import request from '@/utils/request'
import { addleafToArray,queryAreaList,querylikeAreaList } from '@/utils/common';
 // 获取省
export function queryOrgSelectFirst(data) {
    let { level, levelCount, ...param } = data;
    return new Promise(function (resolve) {
        request({
            url: 'static/data/pca-code.json',
            data: param
        }).then((resdata) => {
            console.log("resdata",resdata)
            let resData = resdata.data;
            let orgData=[
                {
                    name:"全部",
                    code:"all"
                }
            ];
            for(let item of resData)
            {
                orgData.push({
                    name:item.name,
                    code:item.code
                });
            }
            if (levelCount == 2 && level == 0) {
                orgData = addleafToArray(orgData, false);
            }
            else if (levelCount == 3 && (level == 0 || level == 1)) {
                orgData = addleafToArray(orgData, false);
            }
            else {
                orgData = addleafToArray(orgData, true);
            }
            console.log("orgData", orgData)
            resolve(orgData);
        })

    })
}

// 获取市县(区)
export function queryOrgSelectNext(data) {
    let { level, levelCount, ...param } = data;
    return new Promise(function (resolve) {
     request({
        url: 'static/data/pca-code.json',
        data:param
    }).then((resdata)=>{
        let orgData = [];
        let resData = resdata.data;
        orgData=queryAreaList(resData,data.code)
        if (levelCount == 2 && level == 1)
        {
            orgData = addleafToArray(orgData, true);
        }
        else if(levelCount == 3 && (level == 0 || level == 1))
        {
            orgData = addleafToArray(orgData, false);
        }
        else
        {
            orgData = addleafToArray(orgData, true);
        }
        console.log("queryOrgSelectNext", orgData)
        resolve(orgData);
    });

   });
}

//模糊搜索省,市,县的数据
export function queryFuzzyOrg(data){
    let { level, levelCount, ...param } = data;
    return new Promise(function (resolve) {
     request({
        url: 'static/data/pca-code.json',
        data:param
    }).then((resdata)=>{
        let orgData = [];
        let resData = resdata.data;
        orgData=querylikeAreaList(resData,data.code,level)
        orgData = addleafToArray(orgData, true);
        console.log("queryFuzzyOrg",orgData)
        resolve(orgData);
    });

   });

}